package com.kfm.spring.service.impl;

import com.kfm.spring.dao.IAccountDao;
import com.kfm.spring.model.Account;
import com.kfm.spring.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional // 开启事务
public class AccountServiceImpl implements IAccountService {

    @Autowired
    private IAccountDao accountDao;

    @Override
    // 事务注解
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
    public void transfer(Integer from, Integer to, Double money) {
        Account source = accountDao.findById(from);
        Account target = accountDao.findById(to);

        // 收钱方
        accountDao.updateMoneyById(to, target.getMoney() + money);
//        int i = 1 / 0;
        accountDao.updateMoneyById(from, source.getMoney() - money);
    }

    @Override
    public void get() {
        System.out.println("get");
    }
}
